package com.cwt_taxi_xa.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cwt_taxi_xa.R;

import java.util.List;

public class GridAdapter extends BaseAdapter {

    private Context mContext = null;
    private List<SettingItem> mSettingData = null;

    public GridAdapter(Context mContext, List<SettingItem> mSettingData) {
        this.mContext = mContext;
        this.mSettingData = mSettingData;
    }

    @Override
    public int getCount() {
        return mSettingData == null? 0:mSettingData.size();
    }

    @Override
    public SettingItem getItem(int position) {
        if(mSettingData != null && mSettingData.size() >1){
            return mSettingData.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new ViewHolder();
            LayoutInflater mInflater = LayoutInflater.from(mContext);
            convertView = mInflater.inflate(R.layout.gridview_item, null);

            viewHolder.logo = (ImageView) convertView.findViewById(R.id.img);
            viewHolder.description = (TextView) convertView.findViewById(R.id.text);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }

        final SettingItem settingItem = getItem(position);
        if (null != settingItem)
        {
            viewHolder.logo.setImageResource(settingItem.getLogoRsId());
            viewHolder.description.setText(settingItem.getDescription());
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    settingItem.getmAdapterOnClickListener().onItemClick(null);
                }
            });
        }

        return convertView;
    }

    private static class ViewHolder
    {
        ImageView logo;
        TextView description;
    }
}
